package com.lemon.smartsafetynew.ui.fragment.record;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.listener.RecycleChildItemClickListener;
import com.lemon.smartsafetynew.entity.me.MeCompanyVo;
import com.lemon.smartsafetynew.entity.record.EmerPlanVo;
import com.lemon.smartsafetynew.net.services.RecordServices;
import com.lemon.smartsafetynew.ui.adapter.record.EmergencyPlanAdapter;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshNoLoadMoreAddTitleFragment;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.common.UrlFormatUtils;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import java.util.ArrayList;
import java.util.List;

import kale.adapter.LemonRcvAdapter;

import static com.thefinestartist.utils.content.ContextUtil.startActivity;

/**
 * Created by JiangAn on 2018/6/6.
 */

public class RecordEmergencyPlanFragment extends BasePullToRefreshNoLoadMoreAddTitleFragment<EmerPlanVo, List<EmerPlanVo>>
        implements RecycleChildItemClickListener<EmerPlanVo> {
    private static MeCompanyVo companyVo;

    public static RecordEmergencyPlanFragment newInstance(MeCompanyVo companyVo) {
        Bundle args = new Bundle();
        RecordEmergencyPlanFragment fragment = new RecordEmergencyPlanFragment();
        args.putSerializable(KeyConstants.KEY_COMPANY_VO, companyVo);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void getBaseArguments() {
        super.getBaseArguments();
        Bundle bundle = getArguments();
        if (bundle != null) {
            if (bundle.containsKey(KeyConstants.KEY_COMPANY_VO)) {
                companyVo = (MeCompanyVo) bundle.getSerializable(KeyConstants.KEY_COMPANY_VO);
            }
        }
    }

    @Override
    protected void initViews() {
        super.initViews();
        llTilte.setVisibility(View.GONE);
        llYellowTitle.setVisibility(View.VISIBLE);
        tvRecordTitle.setText(CommonUtil.avoidNullString(companyVo.getCompany_name()));
    }

    @Override
    public void onPullRefresh() {
        LemonRequestParams params = new LemonRequestParams("");
        params.put("company_id", companyVo.getCompany_id());
        params.put("pageNum",currentPage);
        params.put("pageSize",pageSize);
        RecordServices.init().getContingencyInfo(params);
    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<EmerPlanVo> getLemonAdapter() {
        return new EmergencyPlanAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return KeyConstants.URL_GET_EMERGENCY_PLAN_LIST;
    }

    @Override
    public void onDataList(List<EmerPlanVo> data) {

    }


    @Override
    public void onChildItemClick(EmerPlanVo data, int position) {
        if(TextUtils.isEmpty(data.getFileUrl())){
            Toast.makeText(getContext(),"没有附件",Toast.LENGTH_SHORT).show();
        }else {
            downloadAndShowFile(UrlFormatUtils.formatFiles(data.getFileUrl()),CommonUtil.avoidNullString(data.getFileName()),getContext(),"应急预案");
        }
    }

    @Override
    public void onItemClick(EmerPlanVo data, int position) {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + data.getPhone()));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getContext().startActivity(intent);
    }

    @Override
    public void onLoadMore() {

    }
}





